package com.appsindy.common.util.encryption;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;

public class AESBase64Crypto implements Crypto {
    
    private String key = "p@$$w0rdAPPSINDY";
    
    public AESBase64Crypto(){}
    
    public String encrypt(String paintStr) {
        byte[] raw = key.getBytes();
        
        SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
        Cipher cipher;
        try {
            cipher = Cipher.getInstance("AES");
            cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
            
            byte[] encrypted;
            encrypted = cipher.doFinal(paintStr.getBytes());
            
            String base64 = new sun.misc.BASE64Encoder().encode(encrypted);
            
            return base64;
        } catch (NoSuchAlgorithmException ex) {
            ex.printStackTrace();
        } catch (NoSuchPaddingException ex) {
            ex.printStackTrace();
        } catch (InvalidKeyException ex) {
            ex.printStackTrace();
        } catch (BadPaddingException ex) {
            ex.printStackTrace();
        } catch (IllegalBlockSizeException ex) {
            ex.printStackTrace();
        } finally {
        }
        return null;
    }
    
    public String decrypt(String base64EncryptedStr) {
        byte[] raw = key.getBytes();
        
        SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
        Cipher cipher = null;
        byte[] buf = null;
        try {
            
            buf = new sun.misc.BASE64Decoder().decodeBuffer(base64EncryptedStr);
            
            cipher = Cipher.getInstance("AES");
            try {                
                cipher.init(Cipher.DECRYPT_MODE, skeySpec);
            } catch (InvalidKeyException ex) {
                ex.printStackTrace();
            }
            byte[] transfer;
            
            transfer = cipher.doFinal(buf);
            
            return new String(transfer);
        } catch (IOException ex) {
            ex.printStackTrace();
        } catch (NoSuchPaddingException ex) {
            ex.printStackTrace();
        } catch (NoSuchAlgorithmException ex) {
            ex.printStackTrace();
        } catch (IllegalBlockSizeException ex) {
            ex.printStackTrace();
        } catch (BadPaddingException ex) {
            ex.printStackTrace();
        } finally {
        }
        return null;
    }
    
    public static void main(String[] args) {
        System.out.println(new AESBase64Crypto().encrypt("t@mplus09"));
    //    System.out.println(new AESBase64Crypto().decrypt("hLou0RGCU3wFdlu3zZmLzw=="));
        
    //    System.out.println(new AESBase64Crypto().decrypt(new AESBase64Crypto().encrypt("wampp")));
    }
    
}
